

package yf.mall.service.impl;
import org.springframework.transaction.annotation.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yf.mall.dao.QuestionDao;
import yf.mall.entity.QuestionEntity;
import yf.mall.enums.AnswerStatusEnum;
import yf.mall.enums.QuestionStatusEnum;
import  yf.mall.vo.down.AnswerDown;

import java.util.*;
import java.io.IOException;
import java.net.URLEncoder;
import yf.mall.vo.down.AnswerDown;
import javax.servlet.http.HttpServletResponse;
import java.util.stream.Collectors;
import yf.mall.dao.AnswerDao;
import yf.mall.entity.AnswerEntity;
import yf.mall.vo.AnswerVo;
import yf.mall.service.AnswerService;
import com.yf.service.impl.BaseServiceImpl;

/**
 * 账户记录Service实现类
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Tue Dec 15 13:24:48 CST 2020
 */
@Service
public class AnswerServiceImpl extends BaseServiceImpl<AnswerEntity> implements AnswerService {

    private AnswerDao dao;


    @Autowired
    public void setDao(AnswerDao dao) {
        this.dao = dao;
        super.setBaseDao(dao);
    }

    @Autowired
    private QuestionDao questionDao;



    @Override
    public List<AnswerVo> findVo(AnswerVo vo){
        return dao.findVo(vo);
    }

    @Override
    public AnswerVo getVo(Long id){
        return dao.getVo(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveVo(AnswerVo vo){
        return dao.save(vo.getEntity());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateVo(AnswerVo vo){
        return dao.update(vo.getEntity());
    }



    @Override
    public void downLoad(HttpServletResponse response, List<AnswerVo> list) {
        try {
            List<AnswerDown> datas = list.stream().map(item -> new AnswerDown(item)).collect(Collectors.toList());
            response.setContentType("application/vnd.ms-excel" );
            response.setCharacterEncoding("utf-8" );
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("测试", "UTF-8" );
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx" );
            EasyExcel.write(response.getOutputStream(), AnswerDown.class).sheet("模板" ).doWrite(datas);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }



    /**内部方法**/



    /**外部方法实现**/

    /**
     * 获取产品下的百问百答
     * @param
     * @return
     */
    @Override
    public List<QuestionEntity> getQuestion(List<QuestionEntity> questions) {
        List<QuestionEntity> list = new ArrayList<>();
        if (questions != null && questions.size() > 0){
            for (QuestionEntity question : questions) {
                List<AnswerEntity> answerEntities = this.dao.queryByQuestionId(question.getId(), AnswerStatusEnum.YES.getIndex());
                if (answerEntities != null && answerEntities.size() > 0){
                    question.setList(answerEntities);
                    list.add(question);
                }
            }
        }
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AnswerEntity apiSave(Long userId,QuestionEntity question, String content) {
        AnswerEntity answerEntity = new AnswerEntity();

        answerEntity.setQuestionId(question.getId());
        answerEntity.setTime(new Date());
        answerEntity.setStatus(AnswerStatusEnum.AUDIT.getIndex());
        answerEntity.setContent(content);
        dao.save(answerEntity);

        //如果问题是待审核状态 店铺回答了默认修改为审核通过
        if (question.getStatus() == QuestionStatusEnum.AUDIT.getIndex()){
            question.setStatus(QuestionStatusEnum.YES.getIndex());
            questionDao.update(question);
        }

        return answerEntity;
    }


}
